﻿/*********************************************************************
 * @file       create_week.cpp
 * @brief      周表的编辑和新建界面
 * @details    基于ui文件运行
 * @author     南樟(a2169272059@163.com)
 * @date       2025-2-10
 * @version    1.0.0
 * @copyright  a2169272059@163.com
 *********************************************************************/
#include "create_week.h"
#include "ui_create_week.h"
#include "mainwindow.h"
#include "sqlite.h"
#include <QMessageBox>
/**
 * @brief create_week::create_week 周表编辑界面的构造函数
 * @param parent 父节点
 */
create_week::create_week(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::create_week)
    , m_id(-1)
{
    ui->setupUi(this);
    ui->btn_del->setEnabled(false);
}
/**
 * @brief create_week::init 根据现在的时间初始化数据
 */
void create_week::init()
{
    QDateTime dateTime = QDateTime::currentDateTime();
    ui->timeEdit_begin->setTime(dateTime.time());
    ui->combo_onWeek->setCurrentIndex(dateTime.date().dayOfWeek()-1);
    ui->date_begin->setDate(dateTime.date());
    ui->timeEdit_end->setTime(dateTime.time().addSecs(3600));
    ui->date_end->setDate(dateTime.date().addYears(1));
}
/**
 * @brief create_week::~create_week 周表编辑和新增界面的析构函数
 */
create_week::~create_week()
{
    delete ui;
}
/**
 * @brief create_week::on_back2Week_clicked 标题返回逻辑
 */
void create_week::on_back2Week_clicked()
{
    emit back2Week();
}

/**
 * @brief create_week::on_btn_del_clicked [删除]按钮的返回逻辑
 */
void create_week::on_btn_del_clicked()
{
    emit back2Week();
}

/**
 * @brief create_week::on_btn_cen_clicked [取消]按钮的逻辑
 */
void create_week::on_btn_cen_clicked()
{
    emit back2Week();
}
/**
 * @brief create_week::on_btn_ok_clicked [确认]按钮的逻辑
 */
void create_week::on_btn_ok_clicked()
{
    QString begin_time = ui->timeEdit_begin->time().toString("HH:mm");
    QString end_time = ui->timeEdit_end->time().toString("HH:mm");
    QString begin_date = ui->date_begin->date().toString("yyyy-MM-dd");
    QString end_date = ui->date_end->date().toString("yyyy-MM-dd");

    // 逻辑检查
    if(begin_date > end_date){
        auto tmpWindow = QMessageBox::warning(this,"逻辑错误"
                                              ,"该安排的开始日期比结束日期更早\n交换他们的位置并返回编辑？"
                                              ,QMessageBox::Yes|QMessageBox::Cancel);
        if(tmpWindow == QMessageBox::Yes){
            // 交换俩日期
            ui->date_begin->setDate(QDate::fromString(end_date,"yyyy-MM-dd"));
            ui->date_end->setDate(QDate::fromString(begin_date,"yyyy-MM-dd"));
        }
        return;
    }else if(begin_time>end_time){
        auto tmpWindow = QMessageBox::warning(this,"逻辑错误"
                                              ,"该安排的开始时间比结束时间更早\n交换他们的位置并返回编辑？"
                                              ,QMessageBox::Yes|QMessageBox::Cancel);
        if(tmpWindow == QMessageBox::Yes){
            // 交换俩时间
            ui->timeEdit_begin ->setTime(QTime::fromString(end_time,"HH:mm"));
            ui->timeEdit_end->setTime(QTime::fromString(begin_time,"HH:mm"));
        }
        return;
    }
    if(ui->mode->text() == Mode_createOne){
        db.insert_week_duration(ui->note->text()
                                ,ui->combo_onWeek->currentIndex()+1
                                ,begin_time
                                ,end_time
                                ,begin_date
                                ,end_date);
    }else if(ui->mode->text() == Mode_edit){
        QSqlQuery query(db.get_db());
        query.prepare("UPDATE week_duration "
                      "SET name = :name"
                      ",on_week = :on_week"
                      ",begin_time = :begin_time"
                      ",end_time = :end_time"
                      ",begin_date = :begin_date"
                      ",end_date = :end_date "
                      "WHERE id = :id");
        query.bindValue(":name",ui->note->text());
        query.bindValue(":on_week",ui->combo_onWeek->currentIndex()+1);
        query.bindValue(":begin_time",begin_time);
        query.bindValue(":end_time",end_time);
        query.bindValue(":begin_date",begin_date);
        query.bindValue(":end_date",end_date);
        query.bindValue(":id",m_id);
        if(!query.exec()){
            qDebug()<<"更新周表数据失败"<<query.lastError();
            QMessageBox::warning(g_mainWidget,"错误","更新周表数据失败");
        }
    }else{
        qDebug()<<"非法的日程编辑模式";
    }
    emit back2Week();
}

